package cn.cyh.imeans.bat;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyTest {

    public static void main(String[] args) {
        String source = "https://github.com/roosterkid/openproxylist/blob/main/HTTPS_RAW.txt";
        OkHttpClient client1 = new OkHttpClient.Builder().build();
        Request request1 = new Request.Builder().url(source).addHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
                        "Chrome/91.0.4472.77 Safari/537.36 Edg/91.0')").get().build();
        String res = "";
        try {
            Response response = client1.newCall(request1).execute();
            if (!response.isSuccessful()) {
                return;
            }
            res =response.body().string();
        } catch (IOException e) {
            System.out.println("error:github");
        }

        Pattern pattern = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{1,6})");
        Matcher matcher = pattern.matcher(res);
        List<String> matches = new ArrayList<>();
        while (matcher.find()) {
            matches.add(matcher.group());
        }
        for (String raw : matches) {
            String[] split = raw.split(":");

            Proxy proxy = new Proxy(Proxy.Type.HTTP,
                    new InetSocketAddress(split[0].trim(), Integer.parseInt(split[1].trim())));
            OkHttpClient client = new OkHttpClient.Builder().proxy(proxy).build();
            Request request = new Request.Builder().url("https://www.baidu.com/").build();
            try {
                Response response = client.newCall(request).execute();
                System.out.println("success========:" + raw);
            } catch (IOException e) {
                System.out.println("error:" + raw);
            }
        }
    }
}
